{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to Parameterstudy\n",
    "This notebook shows you a way to set up a bunch, here for, simulations where we vary two parameters. \n",
    "\n",
    "The simulations shall stem from"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "root_dir = '../sample_simulations/2d_streaming_instability/'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and shall be stored in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "project_folder = '../tmp/5001'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use pencilnew internal routines to create this folder later."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import needed packages"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import pencil as pc\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Get our setup done\n",
    "First, lets get the simulation out of root_dir as simulation object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SIM_root = pc.get_sim(root_dir)\n",
    "print('~ Name of the root simulation: ' + SIM_root.name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Second we need to specify the names of our new simulations and their parameter sets, we do this in a nested dictionary:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_sims = {\n",
    "    'AA': {'eps_dtog': 1,\n",
    "           'tausp': 0.1},\n",
    "    \n",
    "    'AB': {'eps_dtog': 1,\n",
    "           'tausp': 0.01},\n",
    "    \n",
    "    'BA': {'eps_dtog': 3,\n",
    "           'tausp': 0.1},\n",
    "    \n",
    "    'BB': {'eps_dtog': 3,\n",
    "           'tausp': 0.01}    \n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The parameters we want to vary are Stokes number (in pencil code named tausp) of the particles and dust-to-gas ratio (named eps_dtog). Both parameters are found to be in the start.in file. Luckly pencilnew does not need to know where to search but looks up pencil code simulation in and local files automatically!\n",
    "\n",
    "But beware, if both run.in and start.in contain a certain parameter you may need to specify in which file exactly you want to modify this parameter!\n",
    "\n",
    "## Create new simulations\n",
    "Now lets create these four new simulations in our new project dir and change parameters accordingly. We do that by producing copies of SIM_root, so check out SIM_root.copy help at this point!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for sim_name in new_sims.keys():            # get simulation name\n",
    "    SIM = SIM_root.copy(path_root=project_folder,\n",
    "                        name=sim_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now go and check out pencil-code/python/tutorials/tmp/5001/"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Change parameters\n",
    "Again we do loop over our new_sims dictionary but this time we change parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SIMs = pc.get_sims(project_folder, quiet=True)\n",
    "for SIM in SIMs:                          # now we iterate over simulations\n",
    "    parameter_set = new_sims[SIM.name]    # get new simulation parameters\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SIM.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "parameter_set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "SIM.change_value_in_file?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
